package com.example.myapplication.module4

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.R
import com.example.myapplication.databinding.FragmentStatisticsBinding
import com.example.myapplication.entity.ItemBean
import com.example.myapplication.module1.ItemGridSpanSizeLookup
import com.example.myapplication.module1.adapter.CategoryContentAdapter
import com.example.mylibrary.base.BaseFragment
import com.example.mylibrary.utils.UiTools

class StatisticsFragment : BaseFragment<FragmentStatisticsBinding>() {
    val mutableList = mutableListOf<Any>()
    val itemBeanList = mutableListOf<ItemBean>()

    private lateinit var statisticsTitle: Array<String>
    private lateinit var statisticsCustomer: Array<String>
    private lateinit var financeStatistics: Array<String>
    override fun getBinding(): FragmentStatisticsBinding {
        return FragmentStatisticsBinding.inflate(layoutInflater)
    }

    private val categoryContentAdapter: CategoryContentAdapter? by lazy {
        activity?.let { CategoryContentAdapter(it) }
    }

    override fun initView(view: View) {

        mutableList.clear()
        statisticsTitle = UiTools.getStringArray(R.array.statisticsTitle)
        statisticsCustomer = UiTools.getStringArray(R.array.statisticsCustomer)
        financeStatistics = UiTools.getStringArray(R.array.financeStatistics)
        val statisticsCustomerImage =
            UiTools.resources.obtainTypedArray(R.array.statisticsCustomerImage)
        val financeStatisticsImage =
            UiTools.resources.obtainTypedArray(R.array.financeStatisticsImage)

        val gridLayoutManager = GridLayoutManager(activity, 4, RecyclerView.VERTICAL, false)

        for (i in statisticsTitle.indices) {
            itemBeanList.clear()
            mutableList.add(statisticsTitle[i])
            when (i) {
                0 -> {
                    for (index in statisticsCustomer.indices) {
                        itemBeanList.add(
                            ItemBean(
                                4,
                                i,
                                index,
                                "4$i$index",
                                statisticsCustomer[index],
                                statisticsCustomerImage.getResourceId(index, 0)
                            )
                        )
                    }
                }
                1 -> {
                    for (index in financeStatistics.indices) {
                        itemBeanList.add(
                            ItemBean(
                                4,
                                i,
                                index,
                                "4$i$index",
                                financeStatistics[index],
                                financeStatisticsImage.getResourceId(index, 0)
                            )
                        )
                    }
                }
            }
            mutableList.addAll(itemBeanList)
        }
        statisticsCustomerImage.recycle()
        financeStatisticsImage.recycle()
        gridLayoutManager.spanSizeLookup = ItemGridSpanSizeLookup(mutableList, 4)
        viewBinding.rvList.layoutManager = gridLayoutManager
        viewBinding.rvList.adapter = categoryContentAdapter

        categoryContentAdapter?.mutableList = mutableList
    }


}
